import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
var _excluded = ["text", "valueType", "mode", "onChange", "renderFormItem", "value", "readonly"];
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import ConfigContext, { useIntl } from '@ant-design/pro-provider';
import { omitUndefined, pickProProps } from '@ant-design/pro-utils';
import { Avatar } from 'antd-v4'; // import type {RangeInputNumberProps,ExtraProps as } from './components/DigitRange'

import { noteOnce } from "rc-util/es/warning";
import React, { useContext } from 'react';
import FieldCascader from './components/Cascader';
import FieldCheckbox from './components/Checkbox';
import FieldCode from './components/Code';
import FieldColorPicker from './components/ColorPicker';
import FieldDatePicker from './components/DatePicker';
import FieldDigit from './components/Digit';
import FieldDigitRange from './components/DigitRange';
import FieldFromNow from './components/FromNow';
import FieldImage from './components/Image';
import FieldIndexColumn from './components/IndexColumn';
import FieldMoney from './components/Money';
import FieldOptions from './components/Options';
import FieldPassword from './components/Password';
import FieldPercent from './components/Percent';
import FieldProgress from './components/Progress';
import FieldRadio from './components/Radio';
import FieldRangePicker from './components/RangePicker';
import FieldRate from './components/Rate';
import FieldSecond from './components/Second';
import FieldSelect, { proFieldParsingText, proFieldParsingValueEnumToArray } from './components/Select';
import FieldStatus from './components/Status';
import FieldSwitch from './components/Switch';
import FieldText from './components/Text';
import FieldTextArea from './components/TextArea';
import FieldTimePicker, { FieldTimeRangePicker } from './components/TimePicker';
import FieldTreeSelect from './components/TreeSelect';
import FieldHOC from './FieldHOC';
var REQUEST_VALUE_TYPE = ['select', 'radio', 'radioButton', 'checkbook'];
/**
 * Render valueType object
 *
 * @param text String | number
 * @param valueType ProColumnsValueObjectType
 */

var defaultRenderTextByObject = function defaultRenderTextByObject(text, valueType, props) {
  var pickFormItemProps = pickProProps(props.fieldProps);

  if (valueType.type === 'progress') {
    return _jsx(FieldProgress, _objectSpread(_objectSpread({}, props), {}, {
      text: text,
      fieldProps: _objectSpread({
        status: valueType.status ? valueType.status : undefined
      }, pickFormItemProps)
    }));
  }

  if (valueType.type === 'money') {
    return _jsx(FieldMoney, _objectSpread(_objectSpread({
      locale: valueType.locale
    }, props), {}, {
      fieldProps: pickFormItemProps,
      text: text,
      moneySymbol: valueType.moneySymbol
    }));
  }

  if (valueType.type === 'percent') {
    return _jsx(FieldPercent, _objectSpread(_objectSpread({}, props), {}, {
      text: text,
      showSymbol: valueType.showSymbol,
      precision: valueType.precision,
      fieldProps: pickFormItemProps,
      showColor: valueType.showColor
    }));
  }

  if (valueType.type === 'image') {
    return _jsx(FieldImage, _objectSpread(_objectSpread({}, props), {}, {
      text: text,
      width: valueType.width
    }));
  }

  return text;
};
/**
 * 根据不同的类型来转化数值
 *
 * @param dataValue
 * @param valueType
 */


var defaultRenderText = function defaultRenderText(dataValue, valueType, props, valueTypeMap) {
  var _props$fieldProps;

  var _props$mode = props.mode,
      mode = _props$mode === void 0 ? 'read' : _props$mode,
      _props$emptyText = props.emptyText,
      emptyText = _props$emptyText === void 0 ? '-' : _props$emptyText;

  if (emptyText !== false && mode === 'read' && valueType !== 'option' && valueType !== 'switch') {
    if (typeof dataValue !== 'boolean' && typeof dataValue !== 'number' && !dataValue) {
      var fieldProps = props.fieldProps,
          render = props.render;

      if (render) {
        return render(dataValue, _objectSpread({
          mode: mode
        }, fieldProps), _jsx(_Fragment, {
          children: emptyText
        }));
      }

      return _jsx(_Fragment, {
        children: emptyText
      });
    }
  } // eslint-disable-next-line no-param-reassign


  delete props.emptyText;

  if (_typeof(valueType) === 'object') {
    return defaultRenderTextByObject(dataValue, valueType, props);
  }

  var customValueTypeConfig = valueTypeMap && valueTypeMap[valueType];

  if (customValueTypeConfig) {
    // eslint-disable-next-line no-param-reassign
    delete props.ref;

    if (mode === 'read') {
      var _customValueTypeConfi;

      return (_customValueTypeConfi = customValueTypeConfig.render) === null || _customValueTypeConfi === void 0 ? void 0 : _customValueTypeConfi.call(customValueTypeConfig, dataValue, _objectSpread(_objectSpread({
        text: dataValue
      }, props), {}, {
        mode: mode || 'read'
      }), _jsx(_Fragment, {
        children: dataValue
      }));
    }

    if (mode === 'update' || mode === 'edit') {
      var _customValueTypeConfi2;

      return (_customValueTypeConfi2 = customValueTypeConfig.renderFormItem) === null || _customValueTypeConfi2 === void 0 ? void 0 : _customValueTypeConfi2.call(customValueTypeConfig, dataValue, _objectSpread({
        text: dataValue
      }, props), _jsx(_Fragment, {
        children: dataValue
      }));
    }
  }

  var needValueEnum = REQUEST_VALUE_TYPE.includes(valueType);
  var hasValueEnum = !!(props.valueEnum || props.request || props.options || ((_props$fieldProps = props.fieldProps) === null || _props$fieldProps === void 0 ? void 0 : _props$fieldProps.options));
  noteOnce(!needValueEnum || hasValueEnum, "\u5982\u679C\u8BBE\u7F6E\u4E86 valueType \u4E3A ".concat(REQUEST_VALUE_TYPE.join(','), "\u4E2D\u4EFB\u610F\u4E00\u4E2A\uFF0C\u5219\u9700\u8981\u914D\u7F6Eoptions\uFF0Crequest, valueEnum \u5176\u4E2D\u4E4B\u4E00\uFF0C\u5426\u5219\u65E0\u6CD5\u751F\u6210\u9009\u9879\u3002"));
  noteOnce(!needValueEnum || hasValueEnum, "If you set valueType to any of ".concat(REQUEST_VALUE_TYPE.join(','), ", you need to configure options, request or valueEnum."));
  /** 如果是金额的值 */

  if (valueType === 'money') {
    return _jsx(FieldMoney, _objectSpread(_objectSpread({}, props), {}, {
      text: dataValue
    }));
  }
  /** 如果是日期的值 */


  if (valueType === 'date') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-MM-DD"
      }, props))
    });
  }
  /** 如果是周的值 */


  if (valueType === 'dateWeek') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-wo",
        picker: "week"
      }, props))
    });
  }
  /** 如果是月的值 */


  if (valueType === 'dateMonth') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-MM",
        picker: "month"
      }, props))
    });
  }
  /** 如果是季度的值 */


  if (valueType === 'dateQuarter') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-\\QQ",
        picker: "quarter"
      }, props))
    });
  }
  /** 如果是年的值 */


  if (valueType === 'dateYear') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY",
        picker: "year"
      }, props))
    });
  }
  /** 如果是日期范围的值 */


  if (valueType === 'dateRange') {
    return _jsx(FieldRangePicker, _objectSpread({
      text: dataValue,
      format: "YYYY-MM-DD"
    }, props));
  }
  /** 如果是日期加时间类型的值 */


  if (valueType === 'dateTime') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldDatePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-MM-DD HH:mm:ss",
        showTime: true
      }, props))
    });
  }
  /** 如果是日期加时间类型的值的值 */


  if (valueType === 'dateTimeRange') {
    // 值不存在的时候显示 "-"
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldRangePicker, _objectSpread({
        text: dataValue,
        format: "YYYY-MM-DD HH:mm:ss",
        showTime: true
      }, props))
    });
  }
  /** 如果是时间类型的值 */


  if (valueType === 'time') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldTimePicker, _objectSpread({
        text: dataValue,
        format: "HH:mm:ss"
      }, props))
    });
  }
  /** 如果是时间类型的值 */


  if (valueType === 'timeRange') {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldTimeRangePicker, _objectSpread({
        text: dataValue,
        format: "HH:mm:ss"
      }, props))
    });
  }

  if (valueType === 'fromNow') {
    return _jsx(FieldFromNow, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'index') {
    return _jsx(FieldIndexColumn, {
      children: dataValue + 1
    });
  }

  if (valueType === 'indexBorder') {
    return _jsx(FieldIndexColumn, {
      border: true,
      children: dataValue + 1
    });
  }

  if (valueType === 'progress') {
    return _jsx(FieldProgress, _objectSpread(_objectSpread({}, props), {}, {
      text: dataValue
    }));
  }
  /** 百分比, 默认展示符号, 不展示小数位 */


  if (valueType === 'percent') {
    return _jsx(FieldPercent, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'avatar' && typeof dataValue === 'string' && props.mode === 'read') {
    return _jsx(Avatar, {
      src: dataValue,
      size: 22,
      shape: "circle"
    });
  }

  if (valueType === 'code') {
    return _jsx(FieldCode, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'jsonCode') {
    return _jsx(FieldCode, _objectSpread({
      text: dataValue,
      language: "json"
    }, props));
  }

  if (valueType === 'textarea') {
    return _jsx(FieldTextArea, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'digit') {
    return _jsx(FieldDigit, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'digitRange') {
    return _jsx(FieldDigitRange, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'second') {
    return _jsx(FieldSecond, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'select' || valueType === 'text' && (props.valueEnum || props.request)) {
    return _jsx(FieldHOC, {
      isLight: props.light,
      children: _jsx(FieldSelect, _objectSpread({
        text: dataValue
      }, props))
    });
  }

  if (valueType === 'checkbox') {
    return _jsx(FieldCheckbox, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'radio') {
    return _jsx(FieldRadio, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'radioButton') {
    return _jsx(FieldRadio, _objectSpread({
      radioType: "button",
      text: dataValue
    }, props));
  }

  if (valueType === 'rate') {
    return _jsx(FieldRate, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'switch') {
    return _jsx(FieldSwitch, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'option') {
    return _jsx(FieldOptions, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'password') {
    return _jsx(FieldPassword, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'image') {
    return _jsx(FieldImage, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'cascader') {
    return _jsx(FieldCascader, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'treeSelect') {
    return _jsx(FieldTreeSelect, _objectSpread({
      text: dataValue
    }, props));
  }

  if (valueType === 'color') {
    return _jsx(FieldColorPicker, _objectSpread({
      text: dataValue
    }, props));
  }

  return _jsx(FieldText, _objectSpread({
    text: dataValue
  }, props));
};

export { defaultRenderText };
export { FieldPercent, FieldIndexColumn, FieldProgress, FieldMoney, FieldDatePicker, FieldRangePicker, FieldCode, FieldTimePicker, FieldText, FieldStatus, FieldSelect, proFieldParsingText, proFieldParsingValueEnumToArray };

var ProField = function ProField(_ref, ref) {
  var _ref2, _fieldProps$value, _ref3;

  var text = _ref.text,
      _ref$valueType = _ref.valueType,
      valueType = _ref$valueType === void 0 ? 'text' : _ref$valueType,
      _ref$mode = _ref.mode,
      mode = _ref$mode === void 0 ? 'read' : _ref$mode,
      _onChange = _ref.onChange,
      renderFormItem = _ref.renderFormItem,
      value = _ref.value,
      readonly = _ref.readonly,
      rest = _objectWithoutProperties(_ref, _excluded);

  var intl = useIntl();
  var context = useContext(ConfigContext);

  var fieldProps = (value !== undefined || _onChange || (rest === null || rest === void 0 ? void 0 : rest.fieldProps)) && _objectSpread(_objectSpread({
    value: value
  }, omitUndefined(rest === null || rest === void 0 ? void 0 : rest.fieldProps)), {}, {
    onChange: function onChange() {
      var _rest$fieldProps, _rest$fieldProps$onCh;

      for (var _len = arguments.length, restParams = new Array(_len), _key = 0; _key < _len; _key++) {
        restParams[_key] = arguments[_key];
      }

      rest === null || rest === void 0 ? void 0 : (_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : (_rest$fieldProps$onCh = _rest$fieldProps.onChange) === null || _rest$fieldProps$onCh === void 0 ? void 0 : _rest$fieldProps$onCh.call.apply(_rest$fieldProps$onCh, [_rest$fieldProps].concat(restParams));
      _onChange === null || _onChange === void 0 ? void 0 : _onChange.apply(void 0, restParams);
    }
  });

  return _jsx(React.Fragment, {
    children: defaultRenderText(mode === 'edit' ? (_ref2 = (_fieldProps$value = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _fieldProps$value !== void 0 ? _fieldProps$value : text) !== null && _ref2 !== void 0 ? _ref2 : '' : (_ref3 = text !== null && text !== void 0 ? text : fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _ref3 !== void 0 ? _ref3 : '', valueType || 'text', _objectSpread(_objectSpread({
      ref: ref
    }, rest), {}, {
      mode: readonly ? 'read' : mode,
      renderFormItem: renderFormItem ? function () {
        var newDom = renderFormItem.apply(void 0, arguments); // renderFormItem 之后的dom可能没有props，这里会帮忙注入一下

        if ( /*#__PURE__*/React.isValidElement(newDom)) return /*#__PURE__*/React.cloneElement(newDom, _objectSpread(_objectSpread({
          placeholder: rest.placeholder || intl.getMessage('tableForm.inputPlaceholder', '请输入')
        }, fieldProps), newDom.props || {}));
        return newDom;
      } : undefined,
      placeholder: rest.placeholder || intl.getMessage('tableForm.inputPlaceholder', '请输入'),
      fieldProps: pickProProps(fieldProps)
    }), context.valueTypeMap)
  });
};

export default /*#__PURE__*/React.forwardRef(ProField);